package ms.jasim.console;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.ActionMap;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import ms.jacrim.framework.XmlConfiguration;
import ms.spm.IAppContext;
import ms.spm.implement.SimpleMethodProvider;
import ms.spm.implement.XmlActionCreator;
import ms.spm.services.ActionCreator;
import ms.utils.XmlFile;

import org.flexdock.util.SwingUtility;

public class TestGui extends JFrame {

	private JasimDockManager dock;
	private ActiveSolutionFrame activeSolution;
	private AgentStatusFrame agStatus;
	private AgentTimelineFrame agTimeline;
	
	private ActionMap actMap;
	protected IAppContext context;

	private XmlConfiguration svcConfig;
	private ActionCreator svcActCreator;
	private JasimClientService svcJasim; 

	public static void main(String args[]) {
		SwingUtility.setPlaf("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestGui frame = new TestGui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	enum Actions {
		SysConnect, SysDisconnect, SysExit, ShowAgentStatus, ShowActiveSolution, ShowAgentTimeline
	}

	public enum MenuCommand {
		EXIT, CONNECT, DISCONNECT, SHOW_ATL, SHOW_AS, SHOW_AgSTATUS
	}

	public TestGui() throws Exception {
		super();

		setupAppContext();

		intializesActions();

		setFont(new Font("Tahoma", Font.PLAIN, 8));
		setTitle("Jasim Console");
		setBounds(100, 100, 540, 402);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		
		setJMenuBar(createJMenuBar());
		
		dock = new FlexDockPanel(this);
		
		add(dock.getContainer());
		
		agTimeline = new AgentTimelineFrame(context);
		dock.setMainView(agTimeline);
	}

	private void setupAppContext() throws Exception {
		context = new ms.spm.implement.SimpleAppContext(null, "$root");

		context.addService(svcConfig = new XmlConfiguration(new XmlFile("conf/jacrim.config.xml")));
		context.addService(svcActCreator = new XmlActionCreator(context));
		context.addService(new SimpleMethodProvider(this, context));
		context.addService(svcJasim = new JasimClientService(context));
	
		svcJasim.startPeriodicUpdate();
	}

	private void intializesActions() {
		actMap = new ActionMap();
		for (Actions act : Actions.values())
			actMap.put(act, svcActCreator.loadAction(act.toString()));
		
		actMap.get(Actions.SysDisconnect).setEnabled(false);
	}

	private JMenuBar createJMenuBar() {
		final JMenuBar menuBar = new JMenuBar();

		final JMenu systemMenu = new JMenu();
		systemMenu.setText("System");
		systemMenu.setMnemonic('S');
		menuBar.add(systemMenu);

		systemMenu.add(new JMenuItem(actMap.get(Actions.SysConnect)));
		systemMenu.add(new JMenuItem(actMap.get(Actions.SysDisconnect)));
		systemMenu.addSeparator();
		systemMenu.add(new JMenuItem(actMap.get(Actions.SysExit)));

		final JMenu windowsMenu = new JMenu();
		windowsMenu.setMnemonic('W');
		windowsMenu.setText("Windows");
		menuBar.add(windowsMenu);

		windowsMenu.add(new JMenuItem(actMap.get(Actions.ShowAgentStatus)));
		windowsMenu.add(new JMenuItem(actMap.get(Actions.ShowActiveSolution)));
		windowsMenu.add(new JMenuItem(actMap.get(Actions.ShowAgentTimeline)));
		return menuBar;
	}
	
		
	void connect(ActionEvent e) throws Exception {
		Actions act = Actions.valueOf(e.getActionCommand());
		if (act == Actions.SysConnect) {
			svcJasim.connect("localhost", 6668);
		}
		else {
			svcJasim.shutdown();
		}
		actMap.get(Actions.SysConnect).setEnabled(!svcJasim.Connected());
		actMap.get(Actions.SysDisconnect).setEnabled(svcJasim.Connected());
	}
	
	void stdAction(ActionEvent e) {
		Actions act = Actions.valueOf(e.getActionCommand());
		switch (act) {
		case SysExit:
			dispose();
			System.exit(0);
			break;
		}
	}
	
	void showAgentWindows(ActionEvent e) throws Exception {
		Actions act = Actions.valueOf(e.getActionCommand());
		switch (act) {
		case ShowAgentStatus:
			if (agStatus == null)
				agStatus = new AgentStatusFrame(context);
			dock.addDockable(agStatus);
			break;
			
		case ShowActiveSolution:
			if (activeSolution == null)
				activeSolution = new ActiveSolutionFrame(context);
			dock.addDockable(activeSolution);
			break;
		case ShowAgentTimeline:
			agTimeline.showBrowser();
		}
	}
	
}
